//
//  ProcessRecordTableControler.m
//  TimeBudget
//
//  Created by isempty.chiu on 2010/9/6.
//  Copyright 2010 LifeMotion. All rights reserved.
//

#import "ProcessRecordTableController.h"
#import "EventController.h"
#import "EventItem.h"
#import "CommonFunc.h"

@implementation ProcessRecordTableController
@synthesize processRecordTable;
@synthesize processRunningLabel;

ProcessRecordTableController	*tableControllerInstance = nil;
+(ProcessRecordTableController*)sharedController
{
	return tableControllerInstance;
}

-(id)init
{
	// Assign self to value returned by super's designated initializer 
	// Designated initializer for NSObject is init 	
	if(tableControllerInstance != nil)
		return nil;
	self = [super init];	
	if(self)
	{
	}
	
	tableControllerInstance = self;
	
	//Notification
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(reloadNotify)
												 name:@"RecordChanged" object:nil];	
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(reloadNotify)
												 name:@"EventChanged" object:nil];	
	return self;
}

- (void)dealloc
{
	//Notification
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	
	//UI Control
	[processRecordTable release];
	[processRunningLabel release];
	
	[super dealloc];
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{	
	int nCount = 0;
	if([EventController sharedController].CurrentEvent)
	{
		nCount = [[EventController sharedController].CurrentEvent.RecordItemList count];
	}
	
	//NSLog(@"return Count = %d", nCount);
	return nCount;
}

- (id)     tableView:(NSTableView *) aTableView
objectValueForTableColumn:(NSTableColumn *) aTableColumn
				 row:(int) rowIndex
{  
	id recordObject = [[EventController sharedController].CurrentEvent.RecordItemList objectAtIndex:rowIndex];
	id value = [recordObject valueForKey:[aTableColumn identifier]];
	return value == nil ? @"None" : value;
}

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject
   forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	//it MUST be comment
	id recordObject = [[EventController sharedController].CurrentEvent.RecordItemList objectAtIndex:rowIndex];
	[recordObject setValue:anObject forKey:@"ProcessComment"];
}

- (void)updateProcessRunning: (NSInteger) duration
{
	int percentage = [EventController sharedController].CurrentEvent.Duration == 0 ? 0 : ((float)duration / ((float)[EventController sharedController].CurrentEvent.Duration)) * 100;
	percentage = percentage > 100 ? 100 : percentage;
	NSString* durationString = [CommonFunc getStringFromTimeInterval:duration];
	[processRunningLabel setStringValue:[NSString stringWithFormat:@"%@ (%d%%)", durationString, percentage]];
	[durationString release];	
}

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
	NSInteger totalDuration = 0;
	NSIndexSet* rowSelectedList = [processRecordTable selectedRowIndexes];
    NSUInteger i = [rowSelectedList firstIndex];
    while (i != NSNotFound)
    {
		totalDuration = totalDuration + [[[[EventController sharedController].CurrentEvent.RecordItemList objectAtIndex:i] valueForKey:@"Duration"] integerValue];
		i = [rowSelectedList indexGreaterThanIndex: i];
    }	
	
	[self updateProcessRunning:totalDuration];	
}

- (void) reloadNotify
{
	NSIndexSet* rowSelectedList = [processRecordTable selectedRowIndexes];
    NSUInteger i = [rowSelectedList firstIndex];
    while (i != NSNotFound)
    {
		[processRecordTable deselectRow:i];
		i = [rowSelectedList indexGreaterThanIndex: i];
    }	
	[self updateProcessRunning:0];
	[processRecordTable reloadData];
}

@end
